import { createSlice } from '@reduxjs/toolkit'
import axios from 'axios'

const channelStore = createSlice({
  name: 'channel',
  // 初始化 state
  initialState: {
    channelList: []
  },
  // 修改数据的方法 【同步，支持直接修改】
  reducers: {
    setChannel(state, action) {
      state.channelList = action.payload
    }
  }
})

const { setChannel } = channelStore.actions

// 异步请求
const fetchChannel = () => {
  return async (dispatch) => {
    let res = await axios.get('http://geek.itheima.net/v1_0/channels')
    dispatch(setChannel(res.data.data.channels))
  }
}

export { fetchChannel }
const reducer = channelStore.reducer
export default reducer